﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RPGServer.Contracts.Updates
{
    /// <summary>
    /// update object status
    /// </summary>
    public class UpdateUOS
    {
        /// <summary>
        /// 处理更新单位的状态信息
        /// </summary>
        public static void Process(RPGPlayer player, byte[] buffer, int length)
        {
            /*
            Scenario	byte	1
            Command	byte	1
            PlayerId	uint	4
            ObjectId	uint	4
            MapId	ushort	2
            CurPosRow	ushort	2
            CurPosCol	ushort	2
            ActionId	byte	1
            DirectionId	byte	1
            TargetMapId	ushort	2
            TargetPosRow	ushort	2
            TargetPosCol	ushort	2
            */

            int offset = 2;
            uint playerId = IntUtil.GetUInt(buffer, ref offset);
            uint objectId = IntUtil.GetUInt(buffer, ref offset);
            ushort mapId = IntUtil.GetUShort(buffer, ref offset);
            ushort curPosRow = IntUtil.GetUShort(buffer, ref offset);
            ushort curPosCol = IntUtil.GetUShort(buffer, ref offset);
            byte actionId = IntUtil.getByte(buffer, ref offset);
            byte directionId = IntUtil.getByte(buffer, ref offset);
            ushort targetMapId = IntUtil.GetUShort(buffer, ref offset);
            ushort targetPosRow = IntUtil.GetUShort(buffer, ref offset);
            ushort targetPosCol = IntUtil.GetUShort(buffer, ref offset);

            player.CurPosRow = curPosRow;
            player.CurPosCol = curPosCol;
            // 判断是否进入战斗
            int randomFight = new Random().Next(0, 10);
            if (randomFight < 3)
            {
                RPGFight fight = new RPGFight();
                fight.Id = PlayerService.getPlayerId();

                // 告诉客户进入战斗
                /*
                Scenario	byte	1
                Command	byte	1
                MapId	ushort	2
                FightId	uint	4
                FightType	byte	1
                objectId	uint	4
                ObjectName	char[16]	16
                UnitId	ushort	2
                HP	ushort	2
                MP	ushort	2
                objectId	uint	4
                ObjectName	char[16]	16
                UnitId	ushort	2
                HP	ushort	2
                MP	ushort	2
                */
                offset = 0;
                byte[] data = new byte[1024];
                IntUtil.SetByte(RPGNET_SCENARIO.SER_MSG, data, ref offset);
                IntUtil.SetByte(RPGNET_MESSAGE.EF, data, ref offset);
                IntUtil.SetUShort(player.CurMapId, data, ref offset);
                IntUtil.SetUInt(fight.Id, data, ref offset);
                IntUtil.SetByte(0x01, data, ref offset);
                Log.Debug("message", "player:{0} ef fightid:{1}", player.CurMapId, fight.Id);
                
                // 玩家的单位列表
                for (int index = 0; index < 10; index++)
                {
                    if (index == 0)
                    {
                        IntUtil.SetUInt((uint)player.RoleId, data, ref offset);
                        IntUtil.SetUInt((uint)player.Id, data, ref offset);
                        IntUtil.SetString("object" + index.ToString(), data, ref offset, 16);
                        IntUtil.SetByte(0x02, data, ref offset);
                        IntUtil.SetUShort(8001, data, ref offset);
                        IntUtil.SetUShort(1000, data, ref offset);
                        IntUtil.SetUShort(10, data, ref offset);

                        fight.Force1[index] = new RPGObject();
                        fight.Force1[index].Player = player;
                        fight.Force1[index].IsPlayer = true;
                        fight.Force1[index].PlayerId = player.Id;
                        fight.Force1[index].ObjectId = player.RoleId;
                        fight.Force1[index].HP = 1000;
                        fight.Force1[index].MP = 10;
                    }
                    else
                    {
                        IntUtil.SetUInt(0, data, ref offset);
                        IntUtil.SetUInt(0, data, ref offset);
                        IntUtil.SetString("", data, ref offset, 16);
                        IntUtil.SetByte(0, data, ref offset);
                        IntUtil.SetUShort(0, data, ref offset);
                        IntUtil.SetUShort(0, data, ref offset);
                        IntUtil.SetUShort(0, data, ref offset);
                    }
                }
                
                // 敌人列表
                for (int index = 1; index <= 5; index++)
                {
                    IntUtil.SetUInt((uint)index, data, ref offset);
                    IntUtil.SetUInt(0, data, ref offset);
                    IntUtil.SetString("Eobject" + index.ToString(), data, ref offset, 16);
                    IntUtil.SetByte(0x01, data, ref offset);
                    IntUtil.SetUShort(8001, data, ref offset);
                    IntUtil.SetUShort(10, data, ref offset);
                    IntUtil.SetUShort(10, data, ref offset);

                    fight.Force2[index] = new RPGObject();
                    fight.Force2[index].IsPlayer = false;
                    fight.Force2[index].PlayerId = 0;
                    fight.Force2[index].ObjectId = (uint)index;
                    fight.Force2[index].HP = 10;
                    fight.Force2[index].MP = 10;
                }

                player.Client.SendData(data, offset);

                CacheData.GetInstance().AddFight(player.CurMapId, fight);
            }
        }
    }
}
